<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>数据字典</title>
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <!--表格头部添加栏-->
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal data-add-btn" lay-event="add">添加</button>
                <button class="layui-btn layui-btn-normal layui-btn-warm" lay-event="dictionariesInit">刷新</button>
                </button>
            </div>
        </script>
        <!--数据表格主体-->
        <table id="dictionaries-table" lay-filter="dictionaries-table"></table>
        <!--右侧修改授权删除-->
        <script type="text/html" id="auth-state">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script src="/static/lib/layui/layui.js" charset="utf-8"></script>
<script>
    layui.extend({
        treeTable: '/static/js/lay-module/treetable-lay/treeTable' //table树形扩展
    }).use(['treeTable', 'form', 'table'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var treeTable = layui.treeTable;
        // 渲染树形表格
        var insTb = treeTable.render({
            elem: '#dictionaries-table',
            toolbar: '#toolbar',
            url: '/dictionaries/list',
            tree: {
                iconIndex: 0,           // 折叠图标显示在第几列
                isPidData: true,        // 是否是id、pid形式数据
                idName: 'id',  // id字段名称
                pidName: 'parentId',     // pid字段名称
                getIcon: function (obj) {
                    return '';
                }
            },
            cols: [
                [
                    {field: 'name', minWidth: '20%', title: '数据名称'},
                    {field: 'value', minWidth: '10%', title: '数据值', align: 'center'},
                    {field: 'type', minWidth: '6%', title: '类型', align: 'center'},
                    {field: 'permission', minWidth: '6%', title: '权限', align: 'center'},
                    {
                        field: 'status', width: '6%', title: '状态', templet: function (res) {
                            var id = res.id;
                            if (res.status == 1) {
                                return "<input type='checkbox'  value = '" + id + "' lay-filter='status' lay-skin='switch' lay-text='开启|禁用' checked>"
                            } else {
                                return "<input type='checkbox'  value = '" + id + "'  lay-filter='status' lay-skin='switch' lay-text='开启|禁用'>"
                            }
                        }
                    },
                    {field: 'describe', minWidth: '15%', title: '描述', align: 'center'},
                    {toolbar: '#auth-state', minWidth: '29%', align: 'center', title: '操作'}
                ]
            ],
            //skin: 'line'
        });

        //执行搜索重载
        var active = {
            reloads: function () {
                //执行重载
                insTb.reload({
                }, 'data');
            }
        };

        /**
         * toolbar监听事件（新增）
         */
        treeTable.on('toolbar(dictionaries-table)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var open = layer.open({
                    title: '添加字典',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['30%', '60%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/dictionaries/show',
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (data) {
                            var files = data.field;
                            $.ajax({
                                type: "post",
                                url: "/dictionaries/save",
                                dataType: "json",
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg(data);
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
            }else if (obj.event === 'dictionariesInit') {  // 监听刷新操作
                $.ajax({
                    type: "get",
                    url: "/dictionaries/init",
                    dataType: "json",
                    success: function (data) {
                        layer.closeAll();
                        layer.msg(data.msg);
                        active.reloads();
                    },
                    error: function (data) {
                        layer.closeAll();
                        layer.msg(data);
                    }
                });
            }
        });

        //状态开关
        form.on('switch(status)', function (obj) {
                  $.ajax({
                      type: "put",
                      url: "/dictionaries/edit",
                      dataType: "json",
                      data: {
                          "id": obj.value,
                          "status": obj.elem.checked ? "1" : "-1"
                      },
                      success: function (data) {
                          layer.closeAll();
                      },
                      error: function (data) {
                          layer.closeAll();
                          layer.msg("错误！请联系管理员");
                      }
                  });
              });

        //操作监听(修改、删除)
        treeTable.on('tool(dictionaries-table)', function (obj) {
            var data = obj.data;
            //修改用户
            if (obj.event === 'edit') {
                var open = layer.open({
                    title: '修改字典',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['30%', '60%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/dictionaries/show?id=' + data.id,
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (data1) {
                            var files = data1.field;
                            files['id'] = data.id;
                            $.ajax({
                                type: "put",
                                url: "/dictionaries/edit",
                                dataType: "json",
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg("错误！请联系管理员");
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
                return false;
            } else if (obj.event === 'delete') {        //删除单个字典
                layer.confirm('确认要删除:' + data.name, function (index) {
                    $.ajax({
                        type: "delete",
                        url: "/dictionaries/delete",
                        dataType: "json",
                        data: {
                            "id": data.id,
                            "parentId": data.parentId
                        },
                        success: function (data) {
                            layer.closeAll();
                            layer.msg(data.msg);
                            if (data.success) {
                                obj.del();
                            }
                        },
                        error: function (data) {
                            layer.closeAll();
                            layer.msg("错误！请联系管理员");
                        }
                    });
                    layer.close(index);
                });
            }
        });

        //加载自定义方法
        $('[data-type]').click(function () {
            var type = $(this).data('type');
            active[type].call(this);
        })
    });
</script>
</body>
</html>